package com.example.redismanual.dao.actionExamples.StreamExamples;

import org.springframework.data.domain.Range;
import org.springframework.data.redis.connection.RedisZSetCommands;
import org.springframework.data.redis.connection.stream.MapRecord;
import org.springframework.data.redis.core.StreamOperations;

import java.util.List;
import java.util.Map;

public class MessageQueue {

    private StreamOperations<String,String,String> streamOperations;
    private String streamKey;

    public MessageQueue(StreamOperations<String, String, String> streamOperations, String streamKey) {
        this.streamOperations = streamOperations;
        this.streamKey = streamKey;
    }

    public String getMessageFromNestedList(List<Object> objects){
        return objects.get(0).toString();
    }

    public void addMessage(Map<String,String> keyValueParis){
        this.streamOperations.add(this.streamKey,keyValueParis);
    }

    public void getMessage(String messId){
        this.streamOperations.range(streamKey, Range.from(Range.Bound.inclusive(messId)).to(Range.Bound.inclusive(messId)));
    }

    public void deleteMessage(String messId){
        this.streamOperations.delete(this.streamKey,messId);
    }

    public long len(){
        return this.streamOperations.size(this.streamKey);
    }

    public List<MapRecord<String,String,String>> getMessageByRange(String staredId, int maxNum){
        return this.streamOperations.range(this.streamKey, Range.leftOpen(staredId,""), RedisZSetCommands.Limit.limit().offset(maxNum));
    }
}
